package com.daykw.wkinfo.task;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.alibaba.fastjson.JSONObject;
import com.daykw.wkinfo.config.NutzDaoConfig;
import com.daykw.wkinfo.http.HttpBase;
import com.daykw.wkinfo.model.ArticleHtml;
import com.daykw.wkinfo.model.ArticleTask;
import com.daykw.wkinfo.utils.SpringContextHolder;

public class ArticleHtmlThread implements Runnable {
	private static Logger logger = Logger.getLogger(ArticleHtmlThread.class);
	ArticleTask task;
	private String wbOrIp = "law.wkinfo.com.cn";// 202.121.166.131:9134
	public ArticleHtmlThread(ArticleTask task) {
		this.task = task;
	}

	@Override
	public void run() {
		NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);
		try {

			dao.update(ArticleTask.class, Chain.make("status", 1), Cnd.where("id", "=", task.getId()));
			Record r = dao.fetch("t_cookie", Cnd.where("id", "=", 2));
			String cookie = r.getString("cookie");
			String url = "http://"+wbOrIp+"/csi/document/" + task.getAid()
					+ "/html?indexId=law.legislation&print=false&fromType=null&useBalance=true";

			HttpBase http = new HttpBase();
			Map<String, String> headers = new HashMap<String, String>();
			headers.put("Accept",
					"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
			headers.put("Accept-Encoding", "gzip, deflate");
			headers.put("Accept-Language", "zh-CN,zh;q=0.9");
			headers.put("Connection", "keep-alive");

			headers.put("Cookie", cookie);
			headers.put("Host", wbOrIp);
			headers.put("Upgrade-Insecure-Requests", "1");
			headers.put("If-None-Match", "W/\"2ac4-ENRxQcAEz3SSTwVcV623+g\"");
			
			headers.put("User-Agent",
					"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");

			String html = http.get(url, "utf-8", headers, false);
			while (html == null || html.equals("fail")) {
				html = http.get(url, "utf-8", headers, false);
			}

			try {
				JSONObject json = (JSONObject) JSONObject.parse(html);

				String content = json.getString("content");
				
				String title = Jsoup.parse(content).select("h1.biao").text();
				
				if(StringUtils.isEmpty(title)) {
					title = Jsoup.parse(content).select("h1.biao-en").text();
				}
				ArticleHtml h = new ArticleHtml();
				h.setTitle(title);
				h.setAid(task.getAid());
				h.setHtml(content);
				h.setCreate_time(new Date());
				h.setUrl(task.getUrl());
				try {
					dao.fastInsert(h);
					logger.info("##############下载完毕,aid:"+task.getAid());
				} catch (Exception e) {
					dao.update(ArticleTask.class, Chain.make("status", 2), Cnd.where("id", "=", task.getId()));
					logger.error("aid:" + task.getAid() + "数据重复");
				}
			} catch (Exception e) {
				dao.update(ArticleTask.class, Chain.make("status", 3), Cnd.where("id", "=", task.getId()));
			}

			logger.info("#############下載完畢aid:" + task.getAid() + "#############");
		} catch (Exception e) {

			dao.update(ArticleTask.class, Chain.make("status", 3), Cnd.where("id", "=", task.getId()));

		}
	}

}
